package com.tencent.service;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.tencent.common.Configure;
import com.tencent.common.Util;
import com.tencent.protocol.user.UserResData;

/**
 * 获取微信用户信息
 * @author tangqh
 * @修改日期 2016-04-07 10:00:00
 */
public class WxUserService {

	/**
	 * 简单方式获取code参数
	 */
	public final static String snsapi_base = "snsapi_base";
	/**
	 * 需要用户确认方式获取code参数 应用授权作用域，snsapi_base
	 * （不弹出授权页面，直接跳转，只能获取用户openid），snsapi_userinfo
	 * （弹出授权页面，可通过openid拿到昵称、性别、所在地。并且，即使在未关注的情况下，只要用户授权，也能获取其信息）
	 */
	public final static String snsapi_userinfo = "snsapi_userinfo";
	
	/**
	 * 创建获取code链接
	 * @param appid 公众号appid
	 * @param url 回调函数url
	 * @param state 回调函数传回参数
	 * @param scope 链接授权作用域
	 */
	public static String getSimpleCodeRequestUrl(String appid, String url, String state, String scope) {
		String GetCodeRequest = Configure.AUTHORIZE_API;
		GetCodeRequest = GetCodeRequest.replace("APPID", Util.urlEnodeUTF8(appid));
		GetCodeRequest = GetCodeRequest.replace("REDIRECT_URI", Util.urlEnodeUTF8(url));
		GetCodeRequest = GetCodeRequest.replace("SCOPE", scope);
		GetCodeRequest = GetCodeRequest.replace("STATE", state);
		return GetCodeRequest;
	}

	/**
	 * 简单方式获取微信用户openid
	 * @param code 微信回调函数返回的code
	 * @return 微信用户openid
	 */
	public static String getOpenIdByCode(String code) {
		String result = "";
		try {
			String url = Configure.ACCESS_TOKEN_API;
			url = url.replace("APPID", Util.urlEnodeUTF8(Configure.getAppid()));
			url = url.replace("SECRET", Util.urlEnodeUTF8(Configure.getAppSecret()));
			url = url.replace("CODE", code);
			HttpGet request = new HttpGet(url);
			HttpResponse response = HttpClients.createDefault().execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity());
				JSONObject jsonObject = new JSONObject(result);
				result = (String) jsonObject.get("openid");
			}
		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 通过微信用户openid获取用户信息
	 * @param code 微信回调函数返回的code
	 * @return 微信用户openid
	 */
	public static UserResData getWxUserByOpenid(String openid) {
		UserResData user = null;
		try {
			String url = Configure.ACCESS_TOKEN_API;
			url = url.replace("ACCESS_TOKEN", WxAccessTokenService.getAccessToken(Configure.getAppid(), Configure.getAppSecret()));
			url = url.replace("OPENID", openid);
			HttpGet request = new HttpGet(url);
			HttpResponse response = HttpClients.createDefault().execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity()));
				user = new UserResData();
				user.setOpenid((String) jsonObject.get("openid"));
				user.setCity((String) jsonObject.get("city"));
				user.setCountry((String) jsonObject.get("country"));
				user.setGroupid((String) jsonObject.get("groupid"));
				user.setHeadimgurl((String) jsonObject.get("headimgurl"));
				user.setLanguage((String) jsonObject.get("language"));
				user.setNickname((String) jsonObject.get("nickname"));
				user.setProvince((String) jsonObject.get("province"));
				user.setRemark((String) jsonObject.get("remark"));
				user.setSex((String) jsonObject.get("sex"));
				user.setSubscribe((String) jsonObject.get("subscribe"));
				user.setSubscribe_time((String) jsonObject.get("subscribe_time"));
				user.setTagid_list((String) jsonObject.get("tagid_list"));
				user.setUnionid((String) jsonObject.get("unionid"));
			}
		} catch (Exception e) {
		}
		return user;
	}

	
}
